package com.example.abc.config;

import com.example.abc.util.JsonUtil;
import com.example.abc.web.IndexController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import java.io.Serializable;

/**
 * Created by Peter on 2018/5/9.
 */
public class MyPermissionEvaluator implements PermissionEvaluator {
    public static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Override
    public boolean hasPermission(Authentication authentication, Object domainObject, Object permission) {
        logger.info(JsonUtil.toJson(authentication));
        logger.info(JsonUtil.toJson(domainObject));
        logger.info(JsonUtil.toJson(permission));
        for(GrantedAuthority ga:authentication.getAuthorities()){
            if(ga.getAuthority().equalsIgnoreCase(permission.toString())){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean hasPermission(Authentication authentication, Serializable serializable, String s, Object o) {
        return false;
    }
}
